<?php
class ControllerDeployApp extends Controller {
    public function index(){

//        unset($this->session->data['app']['deploy_config']);
//        unset($this->session->data['app']['subnet']);
//        unset($this->session->data['app']['vpc']);


        //vpc list
        $this->data['vpc_info']=$this->backend->get_vpc_list($this->session->data['app']['basic']['region'],$this->customer->getEmail(), $this->customer->getPassPlain());


        if(isset($this->session->data['app']['subnet'])){
            foreach($this->session->data['app']['subnet'] as $result){
                $this->data['vpc_info'][$result['vpc_name']][]=$result['subnet_name'];
            }
        }
		//hd list
		// $this->data['hd_info']=$this->backend->get_hd_list($this->session->data['app']['basic']['region'],$this->customer->getEmail(), $this->customer->getPassPlain());
		

        $subnet=array();
        $this->data['subnet_info']=$subnet;


        $this->load->model('tool/image');
        $this->load->model('catalog/product');

        $this->data['products'] = array();

        foreach ($this->cart->getProducts() as $product) {

            if ($product['image']) {
                $image = $this->model_tool_image->resize($product['image'], 75, 75);
            } else {
                $image = '';
            }
            if(isset($this->session->data['app']['hd'][$product['product_id']]['hd_name'])){
                $hd_name=$this->session->data['app']['hd'][$product['product_id']]['hd_name'];
            }else{
                $hd_name="";
            }
            
            if(isset($this->session->data['app']['subnet'][$product['product_id']]['vpc_name'])){
                $vpc_name=$this->session->data['app']['subnet'][$product['product_id']]['vpc_name'];
            }else{
                $vpc_name="";
            }
            if(isset($this->session->data['app']['subnet'][$product['product_id']]['subnet_name'])){
                $subnet_name=$this->session->data['app']['subnet'][$product['product_id']]['subnet_name'];
            }else{
                $subnet_name="";
            }
            if(isset($this->session->data['app']['deploy_app_info'][$product['product_id']])){
                $deploy_app_info=$this->session->data['app']['deploy_app_info'][$product['product_id']];
            }else{
                $deploy_app_info="";
            }
            //newly add 12.16
            $region = "";
            foreach($product['option'] as $key => $value){

                if($value['name'] == 'region'){
                    $region = $value['option_value'];

                }
            }
            //end


            $regions=array();
            foreach ($this->model_catalog_product->getProductOptions($product['product_id']) as $option) {

                if($option['option_description_label']=='region'){
                    foreach($option['option_value'] as $result){
                        $regions[]=array(
                            'value' =>$result['option_value_label'],
                            'name' =>$result['name']
                        );
                    }
                }
            }

            $this->data['products'][] = array(
                'key'                 => $product['key'],
                'product_id'          => $product['product_id'],
                'model'          => $product['model'],
                'name'                => $product['name'],
                'regions'                => $regions,
                'thumb'              => $image,
                'vpc_name'              => $vpc_name,
                'region'                 => $region,
                'hd_name'                 => $hd_name,
                'deploy_app_info'              => $deploy_app_info,
                'subnet_name'              => $subnet_name,
                'href'                => $this->url->link('product/product', 'product_id=' . $product['product_id']),
            );
        }



        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/deploy/app.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/deploy/app.tpl';
        } else {
            $this->template = 'default/template/deploy/app.tpl';
        }

        $this->response->setOutput($this->render());
    }

    public function check_app_name_exist(){
        $appname = $this->request->post['appname'];

        if(!$this->backend->check_app_name_exist($appname, $this->customer->getEmail(), $this->customer->getPassPlain())){
            $json['result']="success";
        }else{
            $json['result']="exist";
        }

        $this->response->setOutput(json_encode($json));
    }

    public function get_vpc_by_region(){
        $region = $this->request->post['region'];
        $app_id = $this->request->post['app_id'];
        $vpc_info=$this->backend->get_vpc_list($region,$this->customer->getEmail(), $this->customer->getPassPlain());
		
		$hd_info=$this->backend->get_hd_list($region,$this->customer->getEmail(), $this->customer->getPassPlain());
		


        //$this->session->data['app']['vpc'][$this->request->post['app_id']];
        //print_r($this->session->data['app']);
        // $vpc_arr = array();
        foreach($this->session->data['app']['vpc'] as $key => $value){
            if($value['region'] == $region){
                foreach($this->session->data['app']['subnet'] as $subnet){
                    if($subnet['vpc_name']==$value['vpc_name']){
                        $vpc_info[$value['vpc_name']][] = $subnet['subnet_name'];
                    }
                }
                

            }

        }
        //print_r($vpc_info);
        //harddisk
         foreach($this->session->data['app']['hd'] as $key => $value){
            if($value['region'] == $region){
            	
                $hd_info[] = $value['hd_name'];

            }

        }
        
        $json =array();

        foreach($vpc_info as $key=> $value){
            //$html+= "<option>$item</option>";
            $json['subnet']='<select class="'.$key.'" aid="'.$app_id.'" name="subnet['.$app_id.']" >';
            $json['subnet'].='<option value="subnet_default">'.$this->language->get('text_select_subnet').'</option>';

            $json['vpc'].="<option value=\"$key\">$key</option>";
            foreach($value as $subnet){
                $json['subnet'].='<option value="'.$subnet.'">'.$subnet.'</option>';
            }
            $json['subnet'].='<option style="font-weight:bold;" value="subnet_creat">'.$this->language->get('text_create_subnet').'</option>';

            $json['subnet'].='</select>';

        }
		foreach($hd_info as $hd){
			
			$json['hd'].="<option value=\"$hd\">$hd</option>";
		}
		//echo $json['subnet'];
		//print_r($vpc_info);
		//print_r($this->session->data['app']['hd']);
		//print_r($this->session->data['app']['deploy_config']);



        $this->response->setOutput(json_encode($json));
    }

    public function get_subnet_by_vpc(){
        $region = $this->request->post['region'];
        $app_id = $this->request->post['app_id'];
        $vpc_name = $this->request->post['vpc_name'];
        $vpc_info=$this->backend->get_vpc_list($region,$this->customer->getEmail(), $this->customer->getPassPlain());


        foreach($this->session->data['app']['vpc'] as $key => $value){
            if($value['region'] == $region){
                foreach($this->session->data['app']['subnet'] as $subnet){
                    if($subnet['vpc_name']==$value['vpc_name']){
                        $vpc_info[$value['vpc_name']][] = $subnet['subnet_name'];
                    }
                }


            }

        }


        $json =array();

        foreach($vpc_info as $key=> $value){

            if($key == $vpc_name){

            $json['subnet'] ='<select class="'.$key.'" aid="'.$app_id.'" name="subnet['.$app_id.']" >';
            $json['subnet'].='<option value="subnet_default">'.$this->language->get('text_select_subnet').'</option>';

            $json['vpc'].="<option value=\"$key\">$key</option>";
            foreach($value as $subnet){
                $json['subnet'].='<option value="'.$subnet.'">'.$subnet.'</option>';
            }
            $json['subnet'].='<option style="font-weight:bold;" value="subnet_creat">'.$this->language->get('text_create_subnet').'</option>';

            $json['subnet'].='</select>';

            }


        }


        $this->response->setOutput(json_encode($json));
    }


}
